package com.baicaoyuan.web.common.lottery;

import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class LotteryUtil {

    public LotteryDomain generateAward(List<LotteryDomain> lotteryDomainList) {

        long result = randomNum(1, 1000);
        int line = 0;
        int temp = 0;
        LotteryDomain returnobj = null;
        int index = 0;
        for (int i = 0; i < lotteryDomainList.size(); i++) {
            LotteryDomain obj2 = lotteryDomainList.get(i);
            int c = obj2.getV();
            temp = temp + c;
            line = 1000 - temp;
            if (c != 0) {
                if (result > line && result <= (line + c)) {
                    returnobj = obj2;
                    break;
                }
            }
        }
        return returnobj;
    }


    private long randomNum(int smin, int smax) {
        int range = smax - smin;
        double rand = Math.random();
        return (smin + Math.round(rand * range));
    }

//    public static void main(String[] args) {
//        List<LotteryDomain> lotteryDomainList = new ArrayList<>();
//        lotteryDomainList.add(new LotteryDomain(1L,"1", 1,"1" ));
//        lotteryDomainList.add(new LotteryDomain(100L,"2", 100,"100" ));
//        lotteryDomainList.add(new LotteryDomain(200L,"3", 100,"200" ));
//        lotteryDomainList.add(new LotteryDomain(500L,"4", 500,"500" ));
//        lotteryDomainList.add(new LotteryDomain(199L,"5", 299,"199" ));
//
//        LotteryUtil l = new LotteryUtil();
//        for (int i = 0; i < 1000; i++) {
//            System.out.println(l.generateAward(lotteryDomainList).getId())  ;
//        }
//
//    }
}
